package com.chenju.invokechainservice.utils;

import com.chenju.invokechainservice.common.enums.EasyCode;
import com.chenju.invokechainservice.common.exceptions.FunnyValidationException;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class FunnyStringUtils extends StringUtils {

    public static String findRealMethodNameFromLambdaMethod(String lambdaMethod) {
        String regex = "lambda\\$(.+?)\\$\\d+";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(lambdaMethod);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return lambdaMethod;
    }

    public static String concatPackageAndDeclarationName(String packageName, String declarationName) {
        return packageName + "." + declarationName;
    }

    public static String findAdviceName(String name) {
        String regex = "\\([^\\)]*\\)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(name);
        if (matcher.find()) {
            return name.replace(matcher.group(), "");
        }
        return name.replace("(", "").replace(")", "");
    }

    public static String findPointcutAnnotationValue(String annotationValue) {
        String regex = "@annotation\\((.*?)\\)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(annotationValue);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return annotationValue;
    }

    public static String findLastName(String fullName) {
        String newFullName = fullName.replace("(", "").replace(")", "");
        int lastIndex = newFullName.lastIndexOf(".");
        if (lastIndex != -1 && lastIndex < newFullName.length() - 1) {
            return newFullName.substring(lastIndex + 1);
        } else {
            return newFullName;
        }
    }

    public static String findDotString(String str) {
        return str.replace("/", ".");
    }

    public static String findPreLevelName(String fullName) {
        String newFullName = fullName.replace("(", "").replace(")", "");
        int lastIndex = newFullName.lastIndexOf(".");
        if (lastIndex != -1 && lastIndex < newFullName.length() - 1) {
            return newFullName.substring(0, lastIndex);
        } else {
            return newFullName;
        }
    }

    public static String joinPath(String... strs) {
        if (strs.length == 0) {
            throw new FunnyValidationException("传入参数个数为0,请检查");
        }

        return Arrays.stream(strs)
                .map(str -> str.replaceAll("[\\\\/]+$", ""))  // 去掉字符串末尾的 / 或 \
                .collect(Collectors.joining(EasyCode.URL_SPLIT));
    }

    public static String getModulePath(String moduleName){
        if (moduleName.contains(":")){
            String[] parts = moduleName.split(":");
            return joinPath(parts);
        }
        return moduleName;
    }

    public static String getClassesFilePath(String buildClassDir){
        return joinPath(buildClassDir, "classes");
    }

    public static void main(String[] args) {
        // System.out.println(joinPath("C:\\Project\\","test\\"));
        System.out.println(getModulePath("abc:vvv"));
    }
}
